package com.dd.ent.car.ui.detail;

import android.content.Context;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.TextView;

import com.dd.ent.car.R;
import com.dd.ent.car.common.bean.comment.Comment;
import com.dd.ent.car.common.http.ImageLoaderUtil;
import com.dd.ent.car.common.util.DateUtils;
import com.dd.ent.car.ui.widget.CircleImageView;


/**
 * Created by dengqu on 2016/8/29.
 */
public class DetailCommentView extends FrameLayout {
    private int position;
    private Comment comment;
    private CircleImageView imgThumb;
    private TextView texUserName;
    private TextView texTime;
    private TextView texLikeCount;
    private TextView textContent;
    private View targetContentView;
    private TextView textTargetContent;
    private OnCommentItemListener onCommentItemListener;

    public DetailCommentView(Context context, AttributeSet attrs) {
        super(context, attrs);
        init(context);
    }

    public DetailCommentView(Context context) {
        super(context);
        init(context);
    }

    private void init(Context context) {
        View root = LayoutInflater.from(context).inflate(R.layout.item_detail_list_comment, this, true);
        imgThumb = (CircleImageView) root.findViewById(R.id.img_thumb);
        texUserName = (TextView) root.findViewById(R.id.tex_user_name);
        texTime = (TextView) root.findViewById(R.id.tex_time);
        texLikeCount = (TextView) root.findViewById(R.id.tex_like_count);
        textContent = (TextView) root.findViewById(R.id.tex_content);
        textTargetContent = (TextView) root.findViewById(R.id.tv_target);
        targetContentView = root.findViewById(R.id.lyt_target);

        root.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                if (onCommentItemListener != null) {
                    onCommentItemListener.onItemClick(position, comment);
                }
            }
        });
    }


    public void refresh(int position, Comment item) {
        this.position = position;
        this.comment = item;
        refreshData();
    }

    private void refreshData() {
        if (comment != null) {
            if (imgThumb != null) {
                ImageLoaderUtil.displayImage(comment.getUserAvatar(), imgThumb);
            }
            if (texUserName != null) {
                texUserName.setText(comment.getUserName());
            }
            if (texTime != null) {
                CharSequence formatTime =
                        DateUtils.formatRelativeTime(comment.getCreateTime());
                texTime.setText(formatTime);
            }
            if (texLikeCount != null) {
                texLikeCount.setText(String.valueOf(comment.getLikeCount()));
            }
            if (textContent != null) {
                textContent.setText(comment.getContent());
            }
            if (comment.getOriginComment() != null) {
                targetContentView.setVisibility(VISIBLE);
                textTargetContent.setText(comment.getOriginComment().getUserName() + "：" + comment.getOriginComment().getContent());

            } else {
                targetContentView.setVisibility(GONE);

            }

        } else {
            targetContentView.setVisibility(GONE);
        }
    }

    public void setOnCommentItemListener(OnCommentItemListener onCommentItemListener) {
        this.onCommentItemListener = onCommentItemListener;
    }

    public interface OnCommentItemListener {
        public void onItemClick(int position, Comment comment);
    }
}
